const {
  $util,
} = getApp();
Component({
  properties: {
    // 属性值可以在组件使用时指定
    cacheName: {
      type: String,
      value: '',
    },
  },
  data: {
    // 私有数据，可用于模板渲染
    page: 1,
    loadmore: false,
    directory: [],
    id: '',
    title: '党组织',
    scrollId: `id0`,
    checkedMemberName: [], // 被选中的党员数组
    allMemChecked: false,
    allOrgChecked: false,
    imgUrl: $util.getImgUrl(),
  },
  methods: {
    // 这里是一个自定义方法
    getList() {
      $util.request({
        url: 'entry/wxapp/partyman.organ.getlist',
        data: {
          id: this.data.id,
          page: this.data.page,
        },
        showLoading: true,
        success: (res) => {
          let list = {};

          // 给返回的组织、党员数据添加checked等于false
          let mem = res.members;
          let org = res.organs;
          for (let i = 0; i < mem.length; i++) {
            mem[i].checked = false;
          }
          for (let i = 0; i < org.length; i++) {
            org[i].checked = false;
          }
          list.members = mem;
          list.organs = org;
          list.mtotal = res.mtotal;

          if (this.data.page > 1) {
            list.members = [...this.data.members, ...list.members];
          }
          
          list.loadmore = res.members.length > 0 ? true : false;
          list.page = res.members.length > 0 ? this.data.page + 1 : this.data.page;

          this.setData(list);
          this.init();
        },
      });
    },
    // 获取 当前组织下所有子组织和党员
    getChildList(e) {
      const {
        id,
        idx,
        title,
      } = e.currentTarget.dataset;
      let {
        directory,
        id: did,
      } = this.data;
      if (id === did) return;
      if (idx !== undefined) {
        directory.splice(idx, directory.length - idx);
      }
      this.setData({
        id: id,
        title: title,
        page: 1,
        keyword: '',
        directory,
      }, () => {
        this.getDirectory();
        this.getList();
      });
    },
    // 获取文件目录
    getDirectory() {
      let directory = this.data.directory;
      let item = [{
        id: this.data.id,
        title: this.data.title,
      }];
      directory = [...directory, ...item];
      this.setData({
        directory,
        scrollId: `id${this.data.id}`,
      });
    },
    saveCache: function(e) {
      // 只 负责 存缓存
      let cacheName = e.currentTarget.dataset.type;
      let id = e.currentTarget.dataset.id;
      let checked = e.currentTarget.dataset.checked;
      let ids = wx.getStorageSync(cacheName) || [];

      // 点击党员将 党员id存入缓存
      if (cacheName === 'memberids') {
        if(!checked){
          if ((ids.indexOf(id) == -1)) {
          ids = ids.concat(id);
          wx.setStorageSync(cacheName, ids);
          }
        } else {
          for (let i = 0; i < ids.length; i++) {
            if (ids[i] == id) {
              ids.splice(i, 1);
              wx.setStorageSync(cacheName, ids);
            } 
          }
        }
        // 获取选中的党员信息
        this.getcheckedMember();
      }
      // 如果点击的是组织  在checked的情况下 还需要 组织id 、选中的组织下的  子组织(接口未返回) 和 所有党员id 也存入缓存
      if (cacheName === 'orgids') {
        let groupids = wx.getStorageSync('orgids') || [];

        if (groupids.indexOf(id) == -1) {
          groupids = groupids.concat(id);
          wx.setStorageSync(cacheName, groupids);
        } else {
          for (let i = 0; i < groupids.length; i++) {
            if (groupids[i] == id) {
              groupids.splice(i, 1);
              wx.setStorageSync(cacheName, groupids);
            } 
          }
        }

        let arr = [];
        arr[0] = parseInt(id);

        $util.request({
          url: 'entry/wxapp/partyman.organ.members',
          data: {
            groupids: JSON.stringify(arr),
            memberids: [],
          },
          showLoading: false,
          success: (res) => {
            let resMem = res.members;
            let cacMem = wx.getStorageSync('memberids') || [];

            if(!checked){
            for (let i = 0; i < resMem.length; i++) {
                if (cacMem.indexOf(resMem[i].inid) == -1) {
                cacMem = cacMem.concat(resMem[i].inid);
                wx.setStorageSync('memberids', cacMem);
                }
              }
              } else {
              for (let i = 0; i < resMem.length; i++) {
                for (let j = 0; j < cacMem.length; j++) {
                  if (resMem[i].inid == cacMem[j]) {
                    cacMem.splice(j, 1);
                  wx.setStorageSync('memberids', cacMem);
                }
                }
              }
            }

            // 获取选中的党员信息
            this.getcheckedMember();
      
          },
        });
      }

    },
    checkAll: function(e) { // 全选
      let cacheName = e.currentTarget.dataset.type;
      let checked = e.currentTarget.dataset.checked;
      let ids = wx.getStorageSync('memberids') || [];

      if (cacheName === 'memberids') {
        let curIds = this.data.members;
        // 点击党员的时候  选中全选的时候
        if (!checked) { // 全选选中
          for (let i = 0; i < curIds.length; i++) {
            if (ids.indexOf(curIds[i].id) == -1) {
              ids = ids.concat(curIds[i].id);
              wx.setStorageSync(cacheName, ids);
            } 
          }
        } else { // 取消全选
          for (let i = 0; i < curIds.length; i++) {
            for (let j = 0; j < ids.length; j++) {
              if (ids[j] == curIds[i].id) {
                ids.splice(j, 1);
                wx.setStorageSync(cacheName, ids);
              } 
            }
          }
        }
        this.getcheckedMember();
      }
      
      // 点击组织全选取消按钮的时候  还需要把它下面的所有组织和党员id都从缓存中删除 
      
    },
    getcheckedMember() { // 获取选中的党员信息
      let memberids = wx.getStorageSync('memberids') || [];

      if (memberids.length > 0) {
        for (let j = 0; j < memberids.length; j++) {
          memberids[j] = parseInt(memberids[j]);
        }
      }
      
      $util.request({
        url: 'entry/wxapp/partyman.organ.members',
        data: {
          groupids: [],
          memberids: JSON.stringify(memberids),
        },
        showLoading: true,
        success: (res) => {
          this.setData({
            checkedMemberName: res.members,
          });

          wx.setStorageSync(this.properties.cacheName, res.members);

          this.init();

        },
      });
    },
    init() {
      this.setData({
        checkedMemberName: wx.getStorageSync(this.properties.cacheName) || [],
      });
      // 更新 组织和党员的 checked状态   对比缓存数据和当前显示的 组织和党员数组
      let curOrg = this.data.organs;// 当前显示的组织
      let curMem = this.data.members;// 当前显示的党员
      let cacheArr_org = wx.getStorageSync('orgids') || [];// 缓存的组织id数组
      let cacheArr_mem = wx.getStorageSync('memberids') || [];// 缓存的党员id数组

      // 将缓存中的组织id和当前页面的组织数组进行对比  添加checked属性  更新组织checked状态
      if (cacheArr_org.length > 0) {
        for (let i = 0; i < curOrg.length; i++) {
          if (cacheArr_org.indexOf(curOrg[i].id) == -1) {
            curOrg[i].checked = false;
            this.setData({
              allOrgChecked: false,
            });
          } else {
            curOrg[i].checked = true;
            this.setData({
              allOrgChecked: true,
            });
          }
        }
      } else {
        for (let i = 0; i < curOrg.length; i++) {
          curOrg[i].checked = false;
          this.setData({
            allOrgChecked: false,
          });
        }
      }

      // 将缓存中的党员id 和 当前页面的党员数组进行对比  添加checked属性  更新党员checked状态
      if (cacheArr_mem.length > 0) {
        for (let j = 0; j < curMem.length; j++) {
          if (cacheArr_mem.indexOf(curMem[j].id) == -1) {
            curMem[j].checked = false;
            this.setData({
              allMemChecked: false,
            });
          } else {
            curMem[j].checked = true;
            this.setData({
              allMemChecked: true,
            });
          }
        }
      } else {
        for (let i = 0; i < curMem.length; i++) {
          curMem[i].checked = false;
          this.setData({
            allMemChecked: false,
          });
        }
      }
      
      this.setData({
        organs: curOrg,
        members: curMem,
      });

    },
    getMoreList() {
      if (this.data.loadmore) this.getList();
    },
  },
  ready() {
    this.getList();
    this.getDirectory();
  },
});